home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Development Platforms / Apple II / Essentials / MPW IIGS Interfaces / AIIGSIncludes / M16.Util < prev    next >
Encoding:
Text File  |  1992-03-04  |  16.2 KB  |  663 lines  |  [TEXT/MPS ]

  1. *************************************************************************
  2. *                                                                        *
  3. *                        Apple //GS Utility Macros                        *
  4. *                                by                                        *
  5. *                            Lou Infeld                                    *
  6. *                                                                        *
  7. *                    Copyright Apple Computer, Inc. 1986-1988            *
  8. *                            All Rights Reserved                            *
  9. *                                                                        *
  10. *    Revision History                                                    *
  11. *    ----------------                                                    *
  12. *    v1.0R1 (04/29/87) --    First Release                                *
  13. *                            This file is a subset of the old            *
  14. *                                'M16.Utility'                            *
  15. *    v2.0R1 (01/22/88) --    First AsmIIGS release                        *
  16. *    v2.0R2 (02/11/88) --    "writelin" renamed "writeline"                *
  17. *    v2.0R3 (03/29/88) --    "|" added to pea instrs to avoid Assembler    *
  18. *                                warning                                    *
  19. *                            "pushlong" was incorrect if &offset was "s"    *
  20. *                                or not specified                        *
  21. *    v2.0R4 (04/14/88) --    "writestr" and "writeline" didn't restore    *
  22. *                            STRING setting                                *
  23. *    v2.0R5 (08/22/88) --    "inc4" and "dec4" redefined to support only    *
  24. *                                positive 4 byte integers                *
  25. *                            "sub4" fixed so that there will not be any    *
  26. *                                truncation warnings generated            *
  27. *                            Pull macros now check for missing parameter *
  28. *                                  or extra parameter                        *
  29. *    v2.0R6 (09/06/88) --    "dec4" fixed to support positive 4 byte        *
  30. *                                integers                                *
  31. *   v2.0R7 (4/4/89) -- changed "long" to "longmx", "short" to "shortmx" *
  32. *                      and added stack based macros (rls3)              *
  33. *                                                                        *
  34. *    v2.1    (11/01/90)    GAB        Added HEX macro                            *
  35. *                                                                        *
  36. *************************************************************************
  37.  
  38. ;............................................................
  39. ;
  40. ;        Pull long (4 bytes) from stack
  41. ;
  42. ;    pulllong address             - pull off stack to address
  43. ;    pulllong addr1,addr2         - pull off stack into 2 places
  44. ;    pulllong [zeropage],offset   - pull off stack and store indirectly             
  45. ;    pulllong                     - pull off stack and leave in A
  46. ;...............................................................
  47.     MACRO
  48.     pulllong &addr1,&addr2
  49.     IF &addr1='' THEN
  50.     IF &addr2≠'' THEN 
  51.         AERROR 'Illegal 2nd parameter'
  52.     ENDIF
  53.         pullword
  54.         pullword
  55.     ELSEIF &addr2='' THEN
  56.         pullword  &addr1
  57.         pullword  &addr1+2
  58.     ELSEIF &addr1[1:1]='[' THEN
  59.         ldy   #&addr2
  60.         pullword  &addr1,y
  61.         ldy   #&addr2+2
  62.         pullword  &addr1,y
  63.     ELSE
  64.         pullword  &addr1
  65.         sta   &addr2
  66.         pullword  &addr1+2
  67.         sta   &addr2+2
  68.     ENDIF
  69.     MEND
  70. ;...............................................................
  71. ;
  72. ;        Pull 3 bytes from stack
  73. ;
  74. ;    pull3 addr    -- pulls bytes off stack and stores in 'loc'
  75. ;...............................................................
  76.     MACRO
  77.     pull3 &addr
  78.     IF &addr='' THEN
  79.         AERROR 'A parameter must be specified'
  80.     ENDIF
  81.         pull1    &addr
  82.         pullword  &addr+1
  83.     MEND
  84. ;...............................................................
  85. ;
  86. ;        Pull 2 bytes from stack
  87. ;
  88. ;    pullword loc     -- pulls bytes off stack and stores in 'loc'
  89. ;    pullword loc,x   -- pulls bytes off stack and stores in 'loc,x'
  90. ;    pullword         -- pulls bytes off stack and leaves in A
  91. ;...............................................................
  92.     MACRO
  93.     pullword &addr,®
  94.         pla
  95.     IF &addr≠'' THEN
  96.         IF ®≠'' THEN
  97.             sta        &addr,®
  98.         ELSE
  99.             sta        &addr
  100.         ENDIF
  101.     ELSEIF ®≠'' THEN 
  102.         AERROR 'Illegal 2nd parameter'
  103.     ENDIF
  104.     MEND
  105. ;...............................................................
  106. ;
  107. ;        Pull 1 byte from stack
  108. ;
  109. ;    pull1 loc     -- pulls byte off stack and stores in 'loc'
  110. ;    pull1 loc,x   -- pulls byte off stack and stores in 'loc,x'
  111. ;    pull1         -- pulls byte off stack and leaves in A
  112. ;...............................................................
  113.     MACRO
  114.     pull1 &addr,®
  115.         shortm
  116.         pullword    &addr,®
  117.         longm
  118.     MEND
  119. ;...............................................................
  120. ;
  121. ;        Push 1 byte onto stack
  122. ;
  123. ;    push1 loc     -- pushes byte onto stack from 'loc'
  124. ;    push1 loc,x   -- pushes byte onto stack from 'loc,x'
  125. ;    push1 #n      -- pushes constant #n onto stack
  126. ;    push1         -- pushes byte onto stack (from A)
  127. ;...............................................................
  128.     MACRO
  129.     push1 &addr,®
  130.         shortm
  131.         pushword &addr,®
  132.         longm
  133.     MEND
  134. ;............................................................
  135. ;
  136. ;        Push long (4 bytes) onto stack
  137. ;
  138. ;    pushlong address             - push contents of address
  139. ;    pushlong address,x           - push contents of address,x
  140. ;    pushlong const,s             - push contents of stack+const    
  141. ;    pushlong #address/const      - push address or constant
  142. ;    pushlong [zeropage],offset   - push using indirect address
  143. ;...............................................................
  144.     MACRO
  145.     pushlong &addr,&offset
  146.     IF &addr[1:1]='#' THEN
  147.         pea        &addr[2:255]>>16
  148.         pea        |&addr[2:255]
  149.     ELSEIF &addr[1:1]='[' THEN
  150.         ldy        #&offset+2
  151.         pushword &addr,y
  152.         ldy        #&offset
  153.         pushword &addr,y
  154.     ELSEIF &offset='s' THEN
  155.         pushword &addr+2,s
  156.         pushword &addr+2,s
  157.     ELSEIF &offset='' THEN
  158.         pushword  &addr+2
  159.         pushword  &addr
  160.     ELSE
  161.         pushword &addr+2,&offset
  162.         pushword &addr,&offset
  163.     ENDIF
  164.     MEND
  165. ;...............................................................
  166. ;
  167. ;        Push 3 bytes onto stack
  168. ;
  169. ;    push3 addr    -- pushes bytes onto stack from 'loc'
  170. ;    push3 addr,x  -- pushes bytes onto stack from 'loc,x'
  171. ;    push3 #n      -- pushes constant #n onto stack
  172. ;...............................................................
  173.     MACRO
  174.     push3 &addr,®
  175.     IF ®≠'' THEN
  176.         lda        &addr+1,®
  177.         pha
  178.         phb
  179.         lda        &addr,®
  180.         sta        1,s
  181.     ELSEIF &addr[1:1]='#' THEN
  182.         lda        #&addr[2:255]>>8
  183.         pha
  184.     GOTO .a
  185.     ELSE
  186.         lda        &addr+1
  187.         pha
  188. .a        phb
  189.         lda        &addr
  190.         sta        1,s
  191.     ENDIF
  192.     MEND
  193. ;...............................................................
  194. ;
  195. ;        Push 2 bytes onto stack
  196. ;
  197. ;    pushword loc     -- pushes bytes onto stack from 'loc'
  198. ;    pushword loc,x   -- pushes bytes onto stack from 'loc,x'
  199. ;    pushword #n      -- pushes constant #n onto stack
  200. ;    pushword         -- pushes bytes onto stack (from A)
  201. ;...............................................................
  202.     MACRO
  203.     pushword &addr,®
  204.     IF &addr[1:1]='#' AND &SETTING('LONGA')='ON' THEN
  205.         pea        |&addr[2:255]
  206.     ELSE
  207.         IF &addr≠'' THEN
  208.             IF ®≠'' THEN 
  209.                 lda        &addr,®
  210.             ELSE
  211.                 lda        &addr
  212.             ENDIF
  213.         ENDIF
  214.         pha
  215.     ENDIF
  216.     MEND
  217. ;...............................................................
  218. ;
  219. ;        Add 2 byte integers
  220. ;
  221. ;    add loc1,loc2,loc3 - adds 'loc1' to 'loc2' and stores in 'loc3'
  222. ;    add loc1,loc2      - adds 'loc1' to 'loc2' and leaves in A
  223. ;    add ,loc2,loc3     - adds A with 'loc2' and stores in 'loc3' 
  224. ;
  225. ;        'loc1' and 'loc2' can be constants
  226. ;...............................................................
  227.          MACRO
  228.     add   &a1,&a2,&a3
  229.     IF &a1≠'' THEN
  230.         lda        &a1
  231.     ENDIF
  232.         clc
  233.         adc        &a2
  234.     IF &a3≠'' THEN
  235.         sta        &a3
  236.     ENDIF
  237.     MEND
  238. ;...............................................................
  239. ;
  240. ;        Add 4 byte integers
  241. ;
  242. ;    add4 loc1,loc2,loc3 - adds 'loc1' to 'loc2' and stores in 'loc3'
  243. ;    add4 ,loc2,loc3     - adds A to 'loc2' and stores in 'loc3'
  244. ;
  245. ;        'loc1' and 'loc2' can be constants
  246. ;...............................................................
  247.     MACRO
  248.     add4    &a1,&a2,&a3
  249.     IF &a1≠'' THEN
  250.         lda        &a1
  251.     ENDIF
  252.         clc
  253.         adc        &a2
  254.         sta        &a3
  255.     IF &a1='' THEN
  256.         lda        #0
  257.     ELSEIF &a1[1:1]='#' THEN
  258.         lda        #^&a1[2:255]
  259.     ELSE
  260.         lda        &a1+2
  261.     ENDIF
  262.     IF &a2[1:1]='#' THEN
  263.         adc        #^&a2[2:255]
  264.     ELSE
  265.         adc        &a2+2
  266.     ENDIF
  267.         sta        &a3+2
  268.     MEND
  269. ;...............................................................
  270. ;
  271. ;        Increment a positive 4 byte integer
  272. ;
  273. ;    inc4 loc - increments 'loc'
  274. ;...............................................................
  275.     MACRO
  276.     inc4  &a1
  277.         inc        &a1
  278.         bne        @a
  279.         inc        &a1+2
  280. @a 
  281.     MEND
  282. ;...............................................................
  283. ;
  284. ;        Subtract 2 byte integers
  285. ;
  286. ;    sub loc1,loc2,loc3 - subtracts 'loc2' from 'loc1' and stores in 'loc3'
  287. ;    sub loc1,loc2      - subtracts 'loc2' from 'loc1' and leaves in A
  288. ;    sub ,loc2,loc3      - subtracts 'loc2' from A and stores in 'loc3' 
  289. ;
  290. ;        'loc1' and 'loc2' can be constants
  291. ;...............................................................
  292.     MACRO
  293.     sub   &a1,&a2,&a3
  294.     IF &a1≠'' THEN
  295.         lda        &a1
  296.     ENDIF
  297.         sec
  298.         sbc        &a2
  299.     IF &a3≠'' THEN
  300.         sta        &a3
  301.     ENDIF
  302.     MEND
  303. ;...............................................................
  304. ;
  305. ;        Subtract 4 byte integers
  306. ;
  307. ;    sub4 loc1,loc2,loc3 - subtracts 'loc2' from 'loc1' and stores in 'loc3'
  308. ;    sub4 ,loc2,loc3     - subtracts 'loc2' from A and stores in 'loc3'
  309. ;...............................................................
  310.     MACRO
  311.     sub4    &a1,&a2,&a3
  312.     IF &a1≠'' THEN
  313.     IF &a1[1:1]='#' THEN
  314.         lda        #<&a1[2:255]
  315.     ELSE
  316.         lda        &a1
  317.     ENDIF
  318.     ENDIF
  319.         sec
  320.     IF &a1[1:1]='#' THEN
  321.         sbc        #<&a2[2:255]
  322.     ELSE
  323.         sbc        &a2
  324.     ENDIF
  325.         sta        &a3
  326.     IF &a1='' THEN
  327.         lda        #0
  328.     ELSEIF &a1[1:1]='#' THEN
  329.         lda        #^&a1[2:255]
  330.     ELSE
  331.         lda        &a1+2
  332.     ENDIF
  333.     IF &a2[1:1]='#' THEN
  334.         sbc        #^&a2[2:255]
  335.     ELSE
  336.         sbc        &a2+2
  337.     ENDIF
  338.         sta        &a3+2
  339.     MEND
  340. ;...............................................................
  341. ;
  342. ;        Decrement a positive 4 byte integer
  343. ;
  344. ;    dec4 loc - decrements 'loc'
  345. ;...............................................................
  346.     MACRO
  347.     dec4  &a1
  348.         pha
  349.         sec
  350.         lda        &a1
  351.         sbc        #1
  352.         bcs        @a
  353.         dec        &a1+2
  354. @a         sta        &a1
  355.         pla
  356.     MEND
  357. ;...............................................................
  358. ;
  359. ;        Define string
  360. ;
  361. ;        Generates a Pascal type of string
  362. ;...............................................................
  363.     MACRO
  364.     str    &string
  365.         lclc    &sset
  366. &sset    SETC &SETTING('STRING')
  367.         STRING PASCAL
  368.         dc.B &string
  369.         STRING &sset
  370.     MEND
  371. ;...............................................................
  372. ;
  373. ;        Define pointer
  374. ;...............................................................
  375.     MACRO
  376.     dp    &pointer
  377.     dc.L &pointer
  378.     MEND
  379. ;...............................................................
  380. ;
  381. ;        Left Shift 4 bytes
  382. ;
  383. ;        asl4  loc,#n                   ;n is shift count (pos)
  384. ;        asl4  loc,lnum                 ;lnum contains shift count (pos)
  385. ;        asl4  loc                      ;X contains shift count (pos)
  386. ;...............................................................
  387.     MACRO
  388.     asl4 &loc,&num
  389.         lda   &loc+2
  390.     IF &num≠'' THEN
  391.         ldx   &num
  392.     ENDIF
  393. @a        asl        a
  394.         asl        &loc
  395.         adc        #0
  396.         dex
  397.         bne        @a
  398.         sta        &loc+2
  399.     MEND
  400. ;...............................................................
  401. ;
  402. ;        Right Shift 4 bytes
  403. ;
  404. ;        lsr4  loc,#n                   ;n is shift count (pos)
  405. ;        lsr4  loc,lnum                 ;lnum contains shift count (pos)
  406. ;        lsr4  loc                      ;X contains shift count (neg)
  407. ;...............................................................
  408.     MACRO
  409.     lsr4  &loc,&num
  410.     IF &num≠'' THEN
  411.         lda        &num
  412.         eor        #$FFFF
  413.         clc
  414.         adc        #1
  415.         tax
  416.     ENDIF
  417.         lda        &loc
  418. @a        lsr        a
  419.         lsr        &loc+2
  420.         bcc        @b
  421.         ora        #$8000
  422. @b        inx
  423.         bne        @a
  424.         sta        &loc
  425.     MEND
  426. ;...............................................................
  427. ;
  428. ;        Turn on native mode
  429. ;
  430. ;    The processor is put into 'native' mode.  8 bit or 16 bit
  431. ;    mode can be specified by using 'short' or 'long' as the
  432. ;    parameter.  If no parameter is specified, 'long' is assumed
  433. ;...............................................................
  434.     MACRO
  435.     native &mode
  436.         clc
  437.         xce
  438.     IF &mode≠'' THEN
  439.         &mode
  440.     ELSE
  441.         longmx
  442.     ENDIF
  443.     MEND
  444. ;...............................................................
  445. ;
  446. ;        Turn on emulation mode
  447. ;
  448. ;    The processor is put into 'emulation' mode.
  449. ;...............................................................
  450.     MACRO
  451.     emulation
  452.         sec
  453.         xce
  454.         longa    off
  455.         longi    off
  456.     MEND
  457. ;...............................................................
  458. ;
  459. ;        Set Memory & registers to 16 bits
  460. ;...............................................................
  461.     MACRO
  462.     longmx
  463.         rep        #%00110000
  464.         longa    on
  465.         longi    on
  466.     MEND
  467. ;...............................................................
  468. ;
  469. ;        Set Memory & A register to 16 bits
  470. ;...............................................................
  471.     MACRO
  472.     longm
  473.         rep        #%00100000
  474.         longa    on
  475.     MEND
  476. ;...............................................................
  477. ;
  478. ;        Set X & Y registers to 16 bits
  479. ;...............................................................
  480.     MACRO
  481.     longx
  482.         rep        #%00010000
  483.         longi    on
  484.     MEND
  485. ;...............................................................
  486. ;
  487. ;        Set Memory & registers to 8 bits
  488. ;...............................................................
  489.     MACRO
  490.     short
  491.         sep        #%00110000
  492.         longa    off
  493.         longi    off
  494.     MEND
  495. ;...............................................................
  496. ;
  497. ;        Set Memory & A register to 8 bits
  498. ;...............................................................
  499.     MACRO
  500.     shortm
  501.         sep        #%00100000
  502.         longa    off
  503.     MEND
  504. ;...............................................................
  505. ;
  506. ;        Set X & Y registers to 8 bits
  507. ;...............................................................
  508.     MACRO
  509.     shortx
  510.         sep        #%00010000
  511.         longi    off
  512.     MEND
  513. ;...............................................................
  514. ;
  515. ;        Write string
  516. ;
  517. ;        writestr loc     - string at loc 'loc'
  518. ;        writestr #'ABC'  - string='ABC'
  519. ;        writestr         - A,Y has loc of string         
  520. ;...............................................................
  521.     MACRO
  522.     writestr &addr
  523.     IF &addr≠'' THEN
  524.         IF &addr[1:1]='#' THEN
  525.             bra        @b
  526.             LCLC    &sset
  527. &sset        SETC    &SETTING('STRING')
  528.             STRING    PASCAL
  529. @a            DC.B    &addr[2:255]
  530.             STRING    &sset
  531. @b            pea        @a>>16
  532.             pea        |@a
  533.         ELSE
  534.             pea        &addr>>16
  535.             pea        |&addr
  536.         ENDIF
  537.     ELSE
  538.         phy
  539.         pha
  540.     ENDIF
  541.         ldx        #$1C0C
  542.         jsl        $E10000
  543.     MEND
  544. ;...............................................................
  545. ;
  546. ;        Write line (string+CR)
  547. ;
  548. ;        writeline loc     - string at loc 'loc'
  549. ;        writeline #'ABC'  - string='ABC'
  550. ;        writeline 'ABC'   - string='ABC'
  551. ;        writeline         - CR only
  552. ;
  553. ;...............................................................
  554.     MACRO
  555.     writeline &addr
  556.     IF &addr≠'' THEN
  557.         IF &addr[1:1]='#' OR &addr[1:1]='''' THEN
  558.             bra        @b
  559.             LCLC    &sset
  560. &sset        SETC    &SETTING('STRING')
  561.             STRING    PASCAL
  562.             IF &addr[1:1]='#' THEN
  563. @a                DC.B    &addr[2:255]
  564.             ELSE
  565. @a                DC.B    &addr[1:255]
  566.             ENDIF
  567.             STRING    &sset
  568. @b            pea        @a>>16
  569.             pea        |@a
  570.         ELSE
  571.             pea        &addr>>16
  572.             pea        |&addr
  573.         ENDIF
  574.     ELSE
  575.         pea        @c+1>>16
  576.         pea        |@c+1
  577.     ENDIF
  578.         ldx        #$1A0C
  579. @c        jsl        $E10000
  580.     MEND
  581. ;...............................................................
  582. ;
  583. ;        Write character
  584. ;
  585. ;        writech          - char in A register
  586. ;        writech #'A'     - char='A'
  587. ;        writech ch,x     - char in loc 'ch,x'
  588. ;...............................................................
  589.     MACRO
  590.     writech &addr,®
  591.     IF &addr≠'' THEN
  592.         IF ®≠'' THEN
  593.             lda        &addr,®
  594.         ELSE
  595.             lda        &addr
  596.         ENDIF
  597.     ENDIF
  598.         pha
  599.         ldx        #$180C
  600.         jsl        $E10000
  601.     MEND
  602. ;...............................................................
  603. ;
  604. ;              Read a char from keyboard
  605. ;
  606. ;        readch addr     - char stored in 'addr'
  607. ;        readch          - char left in A register
  608. ;
  609. ;...............................................................
  610.     MACRO
  611.     readch &addr
  612.         pea        0
  613.         pea        1
  614.         ldx        #$220C
  615.         jsl        $E10000
  616.         pla
  617.     IF &addr≠'' THEN
  618.         sta        &addr
  619.     ENDIF
  620.     MEND
  621.  
  622. ;...............................................................
  623. ;
  624. ; Define a sequence of hexadecimal bytes
  625. ;
  626. ; Usage:
  627. ;
  628. ; [label]        hex        hexstring
  629. ;
  630. ; hexstring must consist of hexadecimal digits ONLY (0..9, A..F, a..f)
  631. ; and must contain an even number of digits.  No spaces, commas, or
  632. ; other characters are allowed.
  633. ;
  634. ; The macro's primary purpose is to allow the definition of pixel
  635. ; maps (such as icons or cursors) while still allowing the programmer
  636. ; to "see" the defined image at the source code level.
  637. ;
  638. ;...............................................................
  639.                  MACRO
  640.                 hex        &arg
  641.                 lclc    &bytes,&hexStr
  642. ; put the arg into a local variable
  643. &hexStr            setc    &arg
  644. ; make sure we've got an even # of bytes
  645.                 if        (&len(&hexStr) AND 1) <> 0 then
  646.                     aerror    'Odd number of bytes in HEX directive'
  647. &hexStr                setc    &concat(&hexstr,'0')
  648.                 endif
  649. ; start the dc.b argument string
  650. &bytes            setc    &concat('$',&substr(&hexStr,1,2))
  651. &hexStr            setc    &substr(&hexStr,3, &len(&hexStr)-2)
  652. ; while there's something in the supplied argument string...
  653.                 while    &len(&hexStr)>0 do
  654. ; copy 2 digits from the string to the dc.b string...
  655. &bytes                setc    &concat(&bytes,',$',&substr(&hexStr,1,2))
  656. ; then remove the first two digits from the supplied string
  657. &hexStr                setc    &substr(&hexStr,3, &len(&hexStr)-2)
  658.                 endwhile
  659. ; produce the actual bytes
  660.                 dc.b    &bytes
  661.                 MEND
  662.  
  663.